package clientapp;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowAdapter;


public class editCustomer extends JFrame implements ActionListener
{
	private JComboBox numberBox = new JComboBox(); 
	private JLabel lblEdit = new JLabel("Select Customer to edit:");
	private String numbers[];
	private JLabel lblName = new JLabel("Name:");
	private JLabel lblSurname = new JLabel("Surname:");
	private JLabel lblTel = new JLabel("Tel Number:");
	private JLabel lblCredit = new JLabel("Available Credit:");
	//create textfields.	
	private JTextField txtName = new JTextField();
	private JTextField txtSurname = new JTextField();
	private JTextField txtTel = new JTextField();
	private JTextField txtCredit = new JTextField();
	//create buttons.
	private JButton btnSetDetails = new JButton("Set Details"); 
	private JButton btnExit = new JButton("Exit"); 
	//create radio buttons
	private JRadioButton rndName = new JRadioButton();
	private JRadioButton rndSurname = new JRadioButton();
	private JRadioButton rndTel = new JRadioButton();
	private JRadioButton rndCredit = new JRadioButton();
	private JRadioButton rndDummy = new JRadioButton();
	private ButtonGroup rndGroup = new ButtonGroup();
	//create panels.
	private JPanel northPanel = new JPanel();
	private JPanel centerPanel = new JPanel();
	private JPanel cPanel = new JPanel();
	private JPanel southPanel = new JPanel();
	private JPanel sPanel = new JPanel();
	
	private JLabel lblRndEdit = new JLabel("Edit..");
	private JPanel Dummy1 = new JPanel();
	private JPanel Dummy2 = new JPanel();
	private JPanel editPnl = new JPanel();
    private Client clientr;
   

	public editCustomer(Client clientp)
	{


		super("Edit Customer Details");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		clientr = (Client)clientp;
		addWindowListener (new WindowAdapter ()
		{
	    	public void windowClosing (WindowEvent e)
	    	{ //overiding windowClosing
				runStore.store.setEnabled(true);
	    	}
		} //the improved WindowListener is added.
		);
		
		setSize(300,220);
		setResizable(false);
		//setResizable(false);
		setLocationRelativeTo(null);
		
		// numbers = new String[Customer.CustomerListSize]; Stu removed this
		
		// Customer.sortCustomersTel(Customer.CustomerList); // stu added this
		
		/*for(int A=0;A<Customer.CustomerListSize;A++)
		{
			numbers[A] = Customer.CustomerList[A].getPhoneNum();
		}*/
		
		//add numbers to combo box
		//Client.sendQuery("EditCustomer1");
		for (int i = 0; i < clientr.getResult().length; i++)
		{
			System.out.println(clientr.getResult(i,0));
			numberBox.addItem(clientr.getResult(i,0));
		}
		//numberBox = new JComboBox(Client.getResult());
		
		//add radio buttons t group 
		
		//setLayout for all Panels
		northPanel.setLayout(new GridLayout(1,2));
		centerPanel.setLayout(new GridLayout(4,2,2,2));
		southPanel.setLayout(new GridLayout(1,2,5,5));
		//add to north panle
		northPanel.add(lblEdit);
		northPanel.add(numberBox);
		numberBox.addActionListener(this);
		//add to south panel
		
		centerPanel.add(lblName);
		centerPanel.add(txtName);
		txtName.setEditable(false);
			
		centerPanel.add(lblSurname);
		centerPanel.add(txtSurname);
		txtSurname.setEditable(false);
		
		centerPanel.add(lblTel);
		centerPanel.add(txtTel);
		txtTel.setEditable(false);
			
		centerPanel.add(lblCredit);
		centerPanel.add(txtCredit);
		txtCredit.setEditable(false);
			
		cPanel.add(centerPanel);
		//add buttons into a south panel
		southPanel.add(btnSetDetails);
		btnSetDetails.addActionListener(this);
		btnSetDetails.setEnabled(false);
		southPanel.add(btnExit);
		btnExit.addActionListener(this);
		sPanel.add(southPanel);
		//add panel to a frame.
		add(northPanel, BorderLayout.NORTH);
		add(cPanel, BorderLayout.CENTER);
		add(sPanel, BorderLayout.SOUTH);
		
		setVisible(true);
	}
	
	public void actionPerformed(ActionEvent ae)
	{
		if(ae.getSource() == btnExit)
		{
			int answer = JOptionPane.showConfirmDialog(null, "Are you show you want to exit",
			   "Exit Application", JOptionPane.YES_NO_OPTION);
			   
			if(answer == JOptionPane.YES_OPTION)
			{
                JOptionPane.showMessageDialog(null, "I'm coming");
				//runStore.store.setEnabled(true);
                this.dispose();
			}
		}
		
		if(ae.getSource() == numberBox)
		{
			String num = (String)numberBox.getSelectedItem();
			String credits = " ";
			
			clientr.sendQuery("EditCustomer2#" + num);
			
			txtName.setText(clientr.getResult(0,0));
			txtSurname.setText(clientr.getResult(0,1));
			txtTel.setText(num);
			txtCredit.setText(clientr.getResult(0,2));
			credits = clientr.getResult(0,2);
			txtName.setEditable(true);
			txtSurname.setEditable(true);
			txtTel.setEditable(true);
			btnSetDetails.setEnabled(true);
				
		}//end if
		
		if(ae.getSource() == btnSetDetails)
		{ 
			String errorMessage = "";
			String newName = txtName.getText();
			String newLast = txtSurname.getText();
			String newPhone = txtTel.getText();
			int custIndex = numberBox.getSelectedIndex();
			
			if(newName.trim().equals("")||newLast.trim().equals("")||newPhone.trim().equals(""))//error handling
			{
				errorMessage = "All fields require input\n";
			}
			//if(newPhone.length()!=10)
			//{
			//	errorMessage += "Phone number must have 10 digits";
			//}
			else //test that phone number characters ranges from 0-9
			{
				try
				{
					Long.parseLong(newPhone);					
					int i = 0;
					boolean isUniquePhoneNum = true;	
				}
				catch(NumberFormatException e)
				{
					errorMessage += "Phone number must contain digits 0-9 only and 18 digits in length";
				}
			}
			if(!errorMessage.equals(""))
			{
				JOptionPane.showMessageDialog(null, errorMessage);
				//reset fields perhaps in textfields				
			}
			else
			{
				clientr.sendQuery("EditCustomer3#" + numberBox.getSelectedItem() + "#" + newName.trim() + "#" + newLast.trim() + "#" + newPhone);
				System.out.println("Client received Final Message = " + clientr.getResult(0,0));
				if (clientr.getResult(0,0).equals("Customer phone number must be a unique phone number"))
				{
					JOptionPane.showMessageDialog(null,"Customer phone number must be a unique phone number");
				}
				else
				{
					JOptionPane.showMessageDialog(null,"Customer has been edited");
					//runStore.store.setEnabled(true);
					//dispose();
				}
			}
		}//else input is correct
	}//btnSetdetails
}//actionPerformed
//class